<?php
	include 'include/functions.php';
	//set session save path
session_save_path($session_path);
// Initialize a session. 
session_start();
$count++;
$sessionId = session_id();
	if ($userID != 1)
	{
		?>
			<SCRIPT language="JavaScript">
				<!--
					window.location="logoff.php";
				//-->
			</SCRIPT>
		<?
	}
	
	//Connect To Database
	dbConnect();
	$SQL = "DELETE FROM AFLPicks WHERE UserID = 7";
	$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error() . "<br/>$SQL<br/>");

	$Num_of_Picks = 0;
	//Loop Through Every Week Of The Season
	for($i = 1; $i < 18; $i++)
	{
		/*Create a Record in "AFLPicks" for this week.
		RandomMonkey's UserID is 7  */
		$SQL = "INSERT INTO AFLPicks (UserID, Week) VALUES (7, $i)";
		$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error() . "<br/>$SQL<br/>");

		//Grab All The Games For The Current Week.
		$SQL = "SELECT Week, GameNum, Home, Away FROM AFLSchedule WHERE Week = $i";
		$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error() . "<br/>$SQL<br/>");

		/*For Each Game In the current week, get a random number, and insert random pick 
		into AFLPicks for that Game.*/
		do
		{
		   if($myrow["Away"] != "")
		   {
			//Get Random Seed
			srand(make_seed());
			//Generate Random 1 or 0
			$random = (rand()%2);
			//Start SQL update Statement
			$GameNum = "Game".$myrow["GameNum"];
			echo "$random<br />";
			$SQL = "UPDATE AFLPicks SET ".$GameNum." = '";
	
			if($random == 0)
				$SQL .= $myrow["Away"];
			else
				$SQL .= $myrow["Home"];

			//Finish SQL UPDATE Statment
			$SQL .= "' WHERE ((Week = $i) AND (UserID = 7))";
			mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error() . "<br/>$SQL<br/>");
			$Num_of_Picks++;
		   }//end IF
		} while($myrow = mysql_fetch_array($result));
		
	}
	
	// Set Same MagicNum for every week
	$SQL = "UPDATE AFLPicks SET MagicNum = 9 WHERE UserID = 7";
	mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error() . "<br/>$SQL<br/>");
	echo "$Num_of_Picks picks made!";
	//Update the number of picks made
	//mysql_query("UPDATE AFLUsers SET PicksMade = $Num_of_Picks WHERE ID = 7") or die("Cannot query the database.<br>" . mysql_error());
